import java.util.ArrayList;

import it.randomtower.engine.World;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class TwoCharacter extends World {
	TiledMap map;
	ArrayList<TwoRobot> players = new ArrayList<TwoRobot>();
	TwoRobot robot;
	boolean characterSelected = false;
	int characterToMove;

	public TwoCharacter(int id, GameContainer container) throws SlickException {
		super(id, container);
	}

	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		super.init(container, game);

		// add the robots
		// used to be in the TwoCharacters() method
		// create the two robots
		players.add(new TwoRobot(0, 0));

		add(players.get(0));

		players.add(new TwoRobot(32, 32));
		add(players.get(1));
		// add(new TwoRobot(32, 32));

		// setup background
		map = new TiledMap("images/simplelevel.tmx");
		loadEntityFromMap(map);
	}

	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		// draw the map
		for (int l = 0; l < map.getLayerCount(); l++) {
			String value = map.getLayerProperty(l, "type", null);
			if (value == null || !value.equalsIgnoreCase("entity")) {
				for (int w = 0; w < map.getWidth(); w++) {
					for (int h = 0; h < map.getHeight(); h++) {
						Image img = map.getTileImage(w, h, l);
						if (img != null) {
							g.drawImage(img, w * img.getWidth(),
									h * img.getHeight());
							
						}
					}
				}
			}
		}

		super.render(container, game, g);
	}

	public boolean isAPlayerAtLocation(int x, int y) {
		for (int i = 0; i <= players.size() - 1; i++) {
			players.get(i).setSelected(false);
			if (players.get(i).isPlayerAtLocation(x, y)) {
				characterToMove = i;
				return true;
			}
		}
		return false;
	}

	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		// super.update(container, game, delta);

		Input input = container.getInput();
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();
		mouseX = (mouseX / 16) * 16;
		mouseY = (mouseY / 16) * 16;
		boolean mousePressed = false;
		
		if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)
				&& mousePressed == false) {
			mousePressed = true;
			// System.out.println("Mouse Pressed");
		}

		if (mousePressed) {

			if (isAPlayerAtLocation(mouseX, mouseY)) {
				players.get(characterToMove).setSelected(true);

			} else {
				if (characterToMove < 0) {
					// Do nothing
				}

				else {
					players.get(characterToMove).movePlayer(mouseX, mouseY);
					characterToMove = -1;
				}
			}
		}
		

		// if (characterSelected == true){
		// //players.get(characterToMove).playerX = mouseX;
		// //players.get(characterToMove).playerY = mouseY;
		// //players.get(characterToMove).update(container, delta);
		//
		// players.get(characterToMove).movePlayer(mouseX, mouseY);
		// characterSelected = false;
		// System.out.println("characterSelected is true");
		// }

		// Need to try: Try making it so you check for both pressed and
		// selected, then move

		// This works... Kinda
		// if (mousePressed == true) {
		// if (characterSelected) {
		// for (int i = 0; i <= players.size() - 1; i++){
		// if(players.get(i).isPlayerAtLocation(mouseX, mouseY))
		// break;
		// else{
		// players.get(characterToMove).movePlayer(mouseX, mouseY);
		// // This, does not workQ
		// // players.get(characterToMove).setPosition(new
		// // Vector2f(mouseX,mouseY));
		// System.out.println("Mouse and Character are True");
		// characterSelected = false;
		// }
		// }
		//
		// } else {
		// for (int i = 0; i <= players.size() - 1; i++) {
		// if (players.get(i).isPlayerAtLocation(mouseX, mouseY)) {
		// // players.get(i).movePlayer(container, players);
		// // players.get(i).movePlayer(mouseX, mouseY);
		// characterSelected = true;
		// characterToMove = i;
		// System.out.println("character selected");
		// break;
		// }
		//
		// }
		// }
		//
		// }
		super.update(container, game, delta);
		//container.sleep(75);

	}

}
